<?php
/* MyMeeting is an online meeting management software.
 * Copyright (C) 2009 Government Of Malaysia
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * @author: Abdullah Zainul Abidin, Nuhaa All Bakry
 *          Eavay Javay Barnad, Sarogini Muniyandi, Teow Jit Huan
 *
 */

class WfmodelsController extends AppController {

    /**
     * Define $name
     *
     */
    var $name = 'Wfmodels';
    /**
     * Define $helpers
     *
     */
    var $helpers = array('Html', 'Form','Javascript');
    

    /**
     * Describe index
     *
     * @param $committee
     * @return null
     */
    function index($committee,$role=null) {
        $dcommittee=$this->dcommittee;
        if(!empty($role)){
            $this->data['Search']['role']=$role;
        }
        if(!empty($this->data['Search']['role']) ){
            $this->set('cursearch_role',$this->data['Search']['role']);
            $wfmodel1=$this->Wfmodel->defaultpermission(1);
            $wfmodels=$this->Wfmodel->committee_role($dcommittee['Committee']['id'],$this->data['Search']['role']);
            $this->set('wfmodels', $wfmodels);
        }
        $this->loadModel('Role');
        $this->set('roles', $this->Role->find('list',array('conditions'=>array('committee_id'=>array(0,$dcommittee['Committee']['id'])))));
    }

    /**
     * Describe view
     *
     * @param $committee
     * @param $id
     * @return null
     */
    function view($committee,$id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid Wfmodel.', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->set('wfmodel', $this->Wfmodel->read(null, $id));
    }

    /**
     * Describe add
     *
     * @param $committee
     * @return null
     */
    function add($committee) {
        $dcommittee=$this->Wfmodel->Committee->findByShortName($committee);
        $this->set('dcommittee',$dcommittee);
        if (!empty($this->data)) {
            $this->Wfmodel->create();
            if ($this->Wfmodel->save($this->data)) {
                $this->Session->setFlash(__('The Wfmodel has been saved', true));
                if(isset($this->data['Wfmodel']['returnpage'])){
                    $this->redirect($this->data['Wfmodel']['returnpage']);
                }
                else{
                    $this->redirect(array('committee'=>$dcommittee['Committee']['short_name'],'controller'=>'wfmodels','action'=>'index'));
                }
            } else {
                $this->Session->setFlash(__('The Wfmodel could not be saved. Please, try again.', true));
            }
        }
        $committees = $this->Wfmodel->Committee->find('list');
        $this->set(compact('committees'));
        $this->set('returnpage',$this->referer(null,true));
    }

    /**
     * Describe edit
     *
     * @param $committee
     * @param $id
     * @return null
     */
    function edit($committee,$id = null) {
        $dcommittee=$this->Wfmodel->Committee->findByShortName($committee);
        $this->set('dcommittee',$dcommittee);
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid Wfmodel', true));
            $this->redirect(array('committee'=>$dcommittee['Committee']['short_name'],'action'=>'index'));
        }
        if (!empty($this->data)) {
            //echo "<pre>".htmlentities(print_r($this->data,true))."</pre>";exit();
            if ($this->Wfmodel->save($this->data)) {
                /*
                if(isset($this->data['Workflow'])){
                    foreach($this->data['Workflow'] as $workdat){
                        $this->Workflow->create();
                        if(!isset($workdat['id'])){
                            $workdat['committee_id']=$dcommittee['Committee']['id'];
                            $workdat['model']=$this->Wfmodel->field('model',array('id'=>$this->data['Wfmodel']['id']));
                        }
                        $this->Workflow->save($workdat);
                    }
                }
                */
                $this->Session->setFlash(__('The Wfmodel has been saved', true));
                $this->redirect(array('action'=>'index','committee'=>$dcommittee['Committee']['short_name'],'controller'=>'wfmodels'));
            } else {
                $this->Session->setFlash(__('The Wfmodel could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Wfmodel->read(null, $id);
        }
        $committees = $this->Wfmodel->Committee->find('list');
        
        $this->set(compact('committees'));
    }

    /**
     * Describe delete
     *
     * @param $committee
     * @param $id
     * @return null
     */
    function delete($committee,$id = null) {
        $dcommittee=$this->Wfmodel->Committee->findByShortName($committee);
        $this->set('dcommittee',$dcommittee);
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for Wfmodel', true));
            $this->redirect(array('action'=>'index','committee'=>$dcommittee['Committee']['short_name']));
        }
        $this->Wfmodel->del($id);
        $this->Session->setFlash(__('Wfmodel deleted', true));
        $this->redirect(array('controller'=>'wfmodels','action'=>'index','committee'=>$dcommittee['Committee']['short_name']));
    }
    
    function update($committee,$role){
        $dcommittee=$this->dcommittee;
        $sppermission=$this->Wfmodel->find('all',array('conditions'=>array('committee_id'=>$dcommittee['Committee']['id'],'role_id'=>$role)));
        if(!empty($sppermission)){
            foreach($sppermission as $s){
                $this->Wfmodel->delete($s['Wfmodel']['id']);
            }
        }
        
        $default=$this->Wfmodel->defaultpermission($role);
        foreach($default as $key=>$d){
            $diff[$key]=array_diff_assoc($this->data[$key],$default[$key]);
        }
        foreach (array_filter($diff) as $key=>$dif){
            foreach($dif as $dcrud=>$dpermission){
                $this->Wfmodel->create();
                $sp['Wfmodel']['model']=$key;
                $sp['Wfmodel']['committee_id']=$dcommittee['Committee']['id'];
                $sp['Wfmodel']['role_id']=$role;
                $sp['Wfmodel']['crud']=$dcrud;
                $sp['Wfmodel']['permission']=$dpermission;
                $this->Wfmodel->save($sp);
            }
        }
        $this->Session->setFlash(__('The Wfmodel has been saved', true));
        $this->redirect(array('controller'=>'wfmodels','action'=>'index','committee'=>$committee,'role'=>$role));
    }
}
?>
